package com.jinrustar.shenjiyingcommon.common.util

import android.annotation.SuppressLint
import android.content.Context
import android.net.ConnectivityManager
import android.net.wifi.WifiManager
import android.os.Build
import android.support.annotation.RequiresApi
import android.telephony.TelephonyManager
import java.io.IOException
import java.net.Inet4Address
import java.net.NetworkInterface
import java.net.SocketException


/**
 * Created by wei on 17-12-29.
 */
object SystemHelper {


    fun exec(cmd: String) {
        try {
            Runtime.getRuntime().exec(cmd)
        } catch (e: IOException) {
            e.printStackTrace()
        }

    }


    fun getIPAddress(context: Context): String? {
        val info = (context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager).activeNetworkInfo
        if (info != null && info.isConnected) {
            if (info.type == ConnectivityManager.TYPE_MOBILE) {//当前使用2G/3G/4G网络
                try {
                    //Enumeration<NetworkInterface> en=NetworkInterface.getNetworkInterfaces();
                    val en = NetworkInterface.getNetworkInterfaces()
                    while (en.hasMoreElements()) {
                        val intf = en.nextElement()
                        val enumIpAddr = intf.inetAddresses
                        while (enumIpAddr.hasMoreElements()) {
                            val inetAddress = enumIpAddr.nextElement()
                            if (!inetAddress.isLoopbackAddress && inetAddress is Inet4Address) {
                                return inetAddress.getHostAddress()
                            }
                        }
                    }
                } catch (e: SocketException) {
                    e.printStackTrace()
                }

            } else if (info.type == ConnectivityManager.TYPE_WIFI) {//当前使用无线网络
                val wifiManager = context.getSystemService(Context.WIFI_SERVICE) as WifiManager
                val wifiInfo = wifiManager.connectionInfo
                return intIP2StringIP(wifiInfo.ipAddress)
            }
        } else {
            //当前无网络连接,请在设置中打开网络
        }
        return null
    }

    /**
     * 将得到的int类型的IP转换为String类型
     *
     * @param ip
     * @return
     */
    fun intIP2StringIP(ip: Int): String {
        return (ip and 0xFF).toString() + "." +
                (ip shr 8 and 0xFF) + "." +
                (ip shr 16 and 0xFF) + "." +
                (ip shr 24 and 0xFF)
    }


    @SuppressLint("MissingPermission")
    fun getIMEI(context: Context): String {
        return (context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager).deviceId

    }

    fun getAndroidId(context: Context): String {

        return android.provider.Settings.Secure.getString(
                context.contentResolver,

                android.provider.Settings.Secure.ANDROID_ID)

    }

    @SuppressLint("MissingPermission")
    fun getSimSerialNumber(context: Context): String {
        return (context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager).simSerialNumber

    }

    fun getSerialNumber1(): String {
        return android.os.Build.SERIAL
    }

    /**
     *
     * getSerialNumber
     *
     * @return result is same to getSerialNumber1()
     */

    fun getSerialNumber(): String? {

        var serial: String? = null

        try {

            val c = Class.forName("android.os.SystemProperties")

            val get = c.getMethod("get", String::class.java)

            serial = get.invoke(c, "ro.serialno") as String

        } catch (e: Exception) {

            e.printStackTrace()

        }

        return serial

    }


    @SuppressLint("MissingPermission")
    @RequiresApi(api = Build.VERSION_CODES.O)
    fun getAndroidDeviceInfo(context: Context): AndroidDeviceInfo {
        return AndroidDeviceInfo(getIMEI(context), getAndroidId(context), getSerialNumber(), getSimSerialNumber(context))
    }


    data class AndroidDeviceInfo(var imei: String, var androidId: String, var sn: String?, var simSn: String)
}